package com.dhcc.DRGsHosp.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DrgsIndexCalApplication;
import com.dhcc.DRGsHosp.facade.DiaAndOperInDRGFacade;
import com.dhcc.DRGsHosp.facade.DrgsIndexCalFacade;

/**
 * @program: DRGsHosp
 * @description: mdc drg下主要手术和主要诊断
 * @author: chenhalin
 * @create: 2019-01-05 10:50
 */
@Named("diaAndOperInDRGFacadeImpl")
public class DiaAndOperInDRGFacadeImpl implements DiaAndOperInDRGFacade {

    @Autowired
    private DrgsIndexCalApplication drgsIndexCalApplication ;

	@Autowired
	private DrgsIndexCalFacade drgsIndexCalFacade ;

	/**
	 *  @Discription：查找mdc下的手术和诊断信息
	 *  @Author： chenhanlin
	 *  @param mdcCode
	 *  @param type 病组层级
	 *  @return
	 *  @Date:2019年01月03日下午1:23:46
	 */
    @Override
    public List<Map<String, Object>> findDiaAndOpeByMdc(String code,String type) {
		// sql参数
		Map<String, Object> condition = new HashMap<>();
		String verRegion = drgsIndexCalFacade.getGroupScheme() ;
		condition.put("verRegion", verRegion) ;

	    StringBuffer sqlB = new StringBuffer() ;
	    // 诊断编码和描述
        List<Map<String,Object>> resultListD = new ArrayList<>() ;
        // 手术编码和描述
        List<Map<String,Object>> resultListO = new ArrayList<>() ;

        condition.put("code",code) ;
        String sql = "" ;
		sqlB.append(" select distinct mdc.mdc_code \"mdcCode\",mdc.mdc_desc \"mdcDesc\",dia.pd0101 \"code\",dia.pd0100 \"desc\",'诊断' \"tp\"") ;
		sqlB.append(" from DRGs.Mr_Baseinfo a ") ;
		sqlB.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex  ") ;
		sqlB.append(" left join drgs.t_patientdrg b on a.pmainindex=b.parid and b.ver_region=:verRegion         ") ;
		sqlB.append(" left join drgs.t_drgs drg on drg.DRG_CODE=b.PARDRG_CODE and b.VER_CODE=drg.VER_CODE         ") ;
		sqlB.append(" left join drgs.t_adrg adrg on adrg.ADRG_CODE=drg.ADRG_CODE and drg.VER_CODE=adrg.VER_CODE         ") ;
		sqlB.append(" left join drgs.t_mdcgroup mdc on mdc.MDC_CODE=adrg.MDC_CODE and mdc.VER_CODE=adrg.VER_CODE ") ;
		sqlB.append(" left join drgs.mr_diagnose dia on dia.pmainindex = a.pmainindex ") ;
		sqlB.append(" left join drgs.mr_operation ope on ope.pmainindex = dia.pmainindex ") ;
		sqlB.append(" where 1=1 ") ;
		sqlB.append(" and dia.pdtype = 'M' ") ;
		sqlB.append(" and b.pardrg_code is not null and ipinfo.p00320000 < 61  \n ") ;
		sql = sqlB.toString() ;
		if("mdc".equals(type)) {
			sql = sql.replace("1=1", "  mdc.mdc_code = :code and 1=1  ");
		} else if("adrg".equals(type)) {
			sql = sql.replace("1=1", "  adrg.adrg_code = :code and 1=1  ");
		} else if("drg".equals(type)) {
			sql = sql.replace("1=1", "  drg.drg_code = :code and 1=1  ");
		}
		resultListD = drgsIndexCalApplication.excuteQueryForList(sql,condition) ;

		sqlB.setLength(0) ;
		sqlB.append("  select distinct mdc.mdc_code \"mdcCode\",mdc.mdc_desc \"mdcDesc\",ope.po0101 \"code\",ope.po0100 \"desc\",'手术'  \"tp\" \n ") ;
		sqlB.append("  from DRGs.Mr_Baseinfo a  \n ") ;
		sqlB.append("  left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ") ;
		sqlB.append("  left join drgs.t_patientdrg b on a.pmainindex=b.parid  and b.ver_region=:verRegion  \n ") ;
		sqlB.append("  left join drgs.t_drgs drg on drg.DRG_CODE=b.PARDRG_CODE and b.VER_CODE=drg.VER_CODE  \n ") ;
		sqlB.append("  left join drgs.t_adrg adrg on adrg.ADRG_CODE=drg.ADRG_CODE and drg.VER_CODE=adrg.VER_CODE         \n ") ;
		sqlB.append("  left join drgs.t_mdcgroup mdc on mdc.MDC_CODE=adrg.MDC_CODE and mdc.VER_CODE=adrg.VER_CODE \n ") ;
		sqlB.append("  left join drgs.mr_diagnose dia on dia.pmainindex = a.pmainindex \n ") ;
		sqlB.append("  left join drgs.mr_operation ope on ope.pmainindex = dia.pmainindex \n ") ;
		sqlB.append("  where 1=1 \n ") ;
		sqlB.append("  and ope.potype = 'M' and ope.po1400 = '1' \n ") ;
		sqlB.append("  and b.pardrg_code is not null and ipinfo.p00320000 < 61  \n ") ;
		sql = sqlB.toString() ;
		if("mdc".equals(type)) {
			sql = sql.replace("1=1", "  mdc.mdc_code = :code and 1=1  ");
		} else if("adrg".equals(type)) {
			sql = sql.replace("1=1", "  adrg.adrg_code = :code and 1=1  ");
		} else if("drg".equals(type)) {
			sql = sql.replace("1=1", "  drg.drg_code = :code and 1=1  ");
		}
		resultListO = drgsIndexCalApplication.excuteQueryForList(sql,condition) ;
        resultListD.addAll(resultListO);
        return resultListD ;
    }

	/**
	 *  @Discription：mdc-adrg-drg ztree数据
	 *  @Author： chenhanlin
     *  @param verCode 分组器版本
     *  @param stdType 标杆类型
	 *  @Date:2019年01月05
	 */
	@Override
	public List<Map<String, Object>> constructMdcAdrgDrg(String verCode,String stdType) {
		// sql参数
		Map<String, Object> condition = new HashMap<>();
		List<Map<String,Object>> resultList = new ArrayList<>() ;
		// 用于保存ztree节点
		List<Map<String,Object>> nodeList = new ArrayList<Map<String,Object>>() ;
		//获取标杆年分
		String stdYear=drgsIndexCalApplication.getStdYear(stdType,verCode,"1970");
	    // 获取标杆定位id
		String stdId = verCode+"_"+stdType+"_"+stdYear;
		condition.put("verCode",verCode) ;
		StringBuffer sqlB = new StringBuffer() ;
		// 用于去除重复数据
		Set<Map<String,Object>> setMap = new HashSet<Map<String,Object>>();
		sqlB.append(" select mdc.mdc_code,mdc.mdc_desc,adrg.adrg_code,adrg.adrg_desc,drg.drg_code,drg.drg_desc, \n" +
				" std.as_weight,std.as_risk_level,std.as_risk_level_desc,drg.ver_code from drgs.t_drgs drg \n" +
				" left join drgs.t_adrg adrg on adrg.adrg_code = drg.adrg_code and drg.ver_code = adrg.ver_code \n" +
				" left join drgs.t_mdcgroup mdc on mdc.mdc_code = adrg.mdc_code and mdc.ver_code = adrg.ver_code \n" +
				" left join ( \n"+
				" 		select std.as_drg_code,std.as_weight,std.as_risk_level ,dd.dic_desc as_risk_level_desc \n"+
				" 		from drgs.T_ANNUALSTD std  \n"+
				" 		left join ( \n"+
				" 			select dd.dic_value,dd.dic_desc from drgs.mr_dictionarydata dd where dd.dic_code = 'RiskLevel' \n"+
				" 		) dd on dd.dic_value = std.as_risk_level \n"+
				" 		where std.as_id = '"+stdId+"'   \n"+
				" ) std on std.as_drg_code = drg.drg_code \n"+
				" where drg.ver_code = :verCode  order by mdc.mdc_code,adrg.adrg_code \n ") ;

		resultList = drgsIndexCalApplication.excuteQueryForList(sqlB.toString(), condition) ;
		for(Map<String, Object> node : resultList) {
			// mdc节点
			Map<String,Object> mdcNode = new HashMap<String,Object>() ;
			// adrg节点
			Map<String,Object> adrgNode = new HashMap<String,Object>() ;
			// drg节点
			Map<String,Object> drgNode = new HashMap<String,Object>() ;
			// ztree 节点赋值
			mdcNode.put("id", node.get("MDC_CODE")) ;
			mdcNode.put("name",node.get("MDC_CODE")+"-"+node.get("MDC_DESC")) ;
			mdcNode.put("pid","0") ;

			adrgNode.put("id", node.get("ADRG_CODE")) ;
			adrgNode.put("name",node.get("ADRG_CODE")+"-"+node.get("ADRG_DESC")) ;
			adrgNode.put("pid",node.get("MDC_CODE")) ;

			String newName = "（权重：";
			//as_weight,std.as_risk_level,std.as_risk_level_desc
			if( !StringUtils.isBlank(""+node.get("AS_WEIGHT")) && !"null".equals(""+node.get("AS_WEIGHT")) ){
				newName += node.get("AS_WEIGHT");
			}
			newName += "，死亡风险等级：";
			if( !StringUtils.isBlank(""+node.get("AS_RISK_LEVEL_DESC")) && !"null".equals(""+node.get("AS_RISK_LEVEL_DESC")) ){
				newName += node.get("AS_RISK_LEVEL_DESC");
			}
			newName += "）"+node.get("DRG_CODE")+"-"+node.get("DRG_DESC");
			
			drgNode.put("id", node.get("DRG_CODE")) ;
			drgNode.put("name",newName) ;
			drgNode.put("pid",node.get("ADRG_CODE")) ;

			// 节点去重
			if(setMap.add(mdcNode)) {
				nodeList.add(mdcNode) ;
			}

			if(setMap.add(adrgNode)) {
				nodeList.add(adrgNode) ;
			}
			if(setMap.add(drgNode)) {
				nodeList.add(drgNode) ;
			}
		}
		return nodeList ;
	}
}
